<?php

// bkt_edit.php
// Michael Baker
// 2010.07.23
//
// Edit thresholding bracket

session_start();

define("IN_MDM", true);
define("CUR_PAGE_TITLE", "Edit thresholding bracket");
define("CUR_FLAG", "bkt.edit");

include '../includes/all.php';
include 'inc/header.php';

if ( !( $_POST["id"] || $_GET["id"] ) ) 
{
  echo ui_error( "No bracket ID supplied" );
  include 'inc/footer.php'; 
  die;
}

$cat_id = ($_POST["id"]) ? $_POST["id"] : $_GET["id"];
if ( $_POST["id"] ) {

  $title = $_POST["title"];
  $mcolor = $_POST["mcolor"];
  
  $dbase->update_thresholding_category( $cat_id, $title, $mcolor );
  
  $data = array();
  
  // Process the post arguments - in the form f1c1_f, f1c1_c, f1c1_f_h, f1c1_c_h
  foreach ( $_POST as $postkey => $postval )
  {
    if ( ( substr_count( $postkey, "_" ) >= 1 ) && ( $postkey[0] == "f" ) )
    {
      $faultNum = $postkey[ 1 ];
      $colorNum = $postkey[ 3 ];
      
      if ( !array_key_exists( $faultNum, $data ) ) 
        $data[ $faultNum ] = array();
      
      if ( !array_key_exists( $colorNum, $data[ $faultNum ] ) )
        $data[ $faultNum ][ $colorNum ] = array();
        
      $newKey = substr( $postkey, 5 );
      $data[ $faultNum ][ $colorNum ][ $newKey ] = $postval;
    }
    
  }
  
  foreach ( $data as $faultNum => $faultArr )
  {
    $thresh_arrs = array();
    foreach ( $faultArr as $colorNum => $colorArr )
    {
      $f = $colorArr[ "f" ];
      $c = $colorArr[ "c" ];
      $f_h = ( array_key_exists( "f_h", $colorArr ) ) ? true : false;
      $c_h = ( array_key_exists( "c_h", $colorArr ) ) ? true : false;
      $c_i = ( array_key_exists( "c_i", $colorArr ) ) ? true : false;
      
      $thresh_arrs[] = array( $f, $c, $f_h, $c_h, $c_i, $colorNum );
    }
    
    $dbase->add_thresholds( $cat_id, $faultNum, $thresh_arrs );
  }
  
  echo ui_info( "Threshold updated" );
}

$cats = $dbase->get_thresholding_categories();

echo "You are currently editing thresholds for: <strong>" . $cats[ $cat_id ] . "</strong>";

?>
<form action="bkt_edit.php" method="post">
  <input type="hidden" name="id" value="<?php echo $cat_id; ?>" />

<?php

echo "<fieldset>\n";
echo "<legend>General</legend>\n";
echo "<div>\n";
echo "<dl><dt><label for=\"title\">Title</label></dt><dd><input name=\"title\" value=\"" . $cats[ $cat_id ] . "\" /></dd></dl>";
echo "<dl><dt><label for=\"color\">Map Color<br />[ <a href=\"http://www.graphviz.org/doc/info/colors.html\" target=\"_new\">help</a> ]</label></dt><dd><input name=\"mcolor\" value=\"" . $dbase->get_thresholding_mapcolor($cat_id) . "\" /></dd></dl>";
echo "</div>\n";
echo "</fieldset>\n";
$f_names = array( "LOSS (frames per sample)", "R. DELAY (seconds)" );

$ctr = 0;
while ( $ctr < count($f_names) )
{
  $f_name = $f_names[ $ctr ];
  
  $fault_id = $ctr;
  echo "<fieldset>\n";
  echo "<legend>#$fault_id: $f_name</legend>\n";
  echo "<div>\n";
  echo "<table class=\"type2p\" cellspacing=\"1\">\n";
  echo str_repeat( "<col class=\"col1\" />", 6 ) . "\n";
  echo "<thead>\n";
  echo "<tr>\n";
  echo "<th>Colour</th>\n";
  echo "<th>Lower bound</th>\n";
  echo "<th>Upper bound</th>\n";
  echo "<th>Exclude lower bound?</th>\n";
  echo "<th>Exclude upper bound?</th>\n";
  echo "<th>Infinite upper bound?</th>\n";
  echo "</tr>\n";
  echo "  " . "</thead>\n";
  
  echo "  " . "<tbody>\n";
  $data = $dbase->get_thresholds( $cat_id, $ctr );
  foreach ( $data as $row ) 
  {
    $floor = $row[ 0 ]; $ceil = $row[ 1 ];
    $floor_h = ($row[ 2 ] ? "checked" : "");
    $ceil_h  = ($row[ 3 ] ? "checked" : "");
    $ceil_i  = ($row[ 4 ] ? "checked" : "");
    $cc      = $row[ 5 ];
    
    $cc_tup  = $dbase->get_colorcode( $cc );
    $td_tag = "<td style=\"background-color: rgb(" . implode(",", $cc_tup) . ");\">";
    
    $prefix = "f" . $fault_id . "c" . $cc . "_";
    
    echo "<tr>\n";
    echo $td_tag . "&nbsp;</td>\n";
    echo "<td style=\"text-align: center\"><input name=\"" . $prefix . "f\" value=\"$floor\" /></td>\n";
    echo "<td style=\"text-align: center\"><input name=\"" . $prefix . "c\" value=\"$ceil\" /></td>\n";
    echo "<td style=\"text-align: center\"><input name=\"" . $prefix . "f_h\" type=\"checkbox\" class=\"permissions-checkbox\" $floor_h /></td>\n";
    echo "<td style=\"text-align: center\"><input name=\"" . $prefix . "c_h\" type=\"checkbox\" class=\"permissions-checkbox\" $ceil_h /></td>\n";
    echo "<td style=\"text-align: center\"><input name=\"" . $prefix . "c_i\" type=\"checkbox\" class=\"permissions-checkbox\" $ceil_i /></td>\n";
    echo "</tr>\n";
  }
  
  echo "</tbody>\n";
  
  echo "</table>\n";
  echo "</div>\n";
  echo "</fieldset>\n";
  
  $ctr++;
}
  

?>

<input type="submit" value="Apply" class="button2" />

</form>

<?php include 'inc/footer.php'; ?>